﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.InnerReturn;
using MINERP.InerReturn.IView;
using MINERP.MODEL.FoundationModel;
using System.Data;

namespace MINERP.InerReturn.Presenter
{
    public class MainPresenter
    {
        BLL.InnerReturn.InnerReturnBLL _bll = new InnerReturnBLL();

        private IFrmMain _IView;
        public MainPresenter(IFrmMain view)
        {

            _IView = view;
        }
        public void init()
        {
            _IView.DisplayAllTech(_bll.GetAllTech());
            _IView.BangDingUsers(_bll.GetAllUsers());
        }

        public void GetPhotoView()
        {
            //if (_IView.CurrBarCode=="")
            //{
            //    MessageBox.Show("请输入条码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;  
            //}
            if(_bll.IsFandanIng(_IView.CurrBarCode))
            {
                MessageBox.Show("该照片已经在返工中，或返工未做完！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            PhotoView photoView = _bll.GetPhotoViewByBarCode(_IView.CurrBarCode);

            if (photoView == null)
            {
                MessageBox.Show("照片不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (_IView.CurrPhoto.Count > 0)
            {

                if (_IView.CurrPhoto[0].OrderProductId != photoView.OrderProductId)
                {
                    MessageBox.Show("不属于同一产品的照片不能一起开返工单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }


            _IView.CurrTech = _bll.GetCurrTech(photoView.OrderProductId);
            if (_IView.CurrTech.TechId==0)
            {
                MessageBox.Show("该产品已经结束生成，不能开内部返工单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            PhotoView photoViewFind = _IView.CurrPhoto.Find(p => p.PhotoId == photoView.PhotoId);
            if (photoViewFind!=null)
            {
                MessageBox.Show("该照片已经在列表中，请不要重复添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            _IView.CurrPhoto.Add(photoView);//添加照片列表
            _IView.CurrProductId = photoView.OrderProductId;//产品
            _IView.BanDingPhotos(_IView.CurrPhoto);//刷新照片
            _IView.BangDingTech(GetReturnTech(_IView.CurrTech.TechId), _IView.CurrTech.TechName);
        }
        /// <summary>
        /// 获取返工列表
        /// </summary>
        /// <param name="inEndOrder"></param>
        /// <returns></returns>
        public List<Tb_Tech_Info> GetReturnTech(int inEndOrder)
        {
            if (_IView.CurrPhoto.Count > 0)
            {
                return _bll.GetReturnList(inEndOrder, _IView.CurrPhoto[0].OrderProductId);
            }
            else
            {
                return null;
            }

        }
       

        /// <summary>
        /// 创建返单
        /// </summary>
        /// <param name="infos"></param>
        /// <param name="inReturnUid"></param>
        /// <param name="inStrRemark"></param>
        public void CreateReturnOrder(List<Tb_Tech_Info> infos,  string inStrRemark, int inReturnTechId)
        {
            _bll.CreateReturnOrder(_IView.CurrProductId, infos, _IView.CurrPhoto, _IView.CurrUserId,_IView.CurrErrUserId, inStrRemark, inReturnTechId, _IView.OriginateUserID);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inTechId"></param>
        /// <returns></returns>
        public User_Info GetUser(int inTechId)
        {

            if (_IView.CurrProductId != 0)
            {
                return _bll.GetUser(_IView.CurrProductId, inTechId);
            }
            else
            {
                return new User_Info() {UserID = 0, UserName ="0"};
            }
        }
        public List<User_Info> GetUsersByTechId(int inTechId)
        {
            return _bll.GetUsersByTech(inTechId);
        }
        public DataTable GetUnComplteFanDan()
        {
            return _bll.GetUnComplteFanDan();
        }
        public DataTable GetPhotosByProductID(int inProductId)
        {
            return _bll.GetPhotosByProductID(inProductId);
        }


        public int  ActionMainProduct(int inChildProductID)
        {
           return  _bll.ActionMainProduct(inChildProductID);
        }
    }
}
